﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESoft.Pool;
using RabbitMQ.Client;

namespace ESoft.RabbitMQ
{
    /// <summary>
    /// 管道池
    /// </summary>
    public class ChannelPool : ObjectPool<IModel>, IChannelObjectPool
    {
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="rabbitFactory"></param>
        public ChannelPool(IRabbitFactory rabbitFactory) : base(rabbitFactory.CreateChannel)
        {

        }

        /// <summary>
        /// 取出对象
        /// </summary>
        /// <returns></returns>
        public new IModel Get()
        {
            if (Objects.TryTake(out IModel item))
            {
                if (item != null)
                {
                    if (item.IsOpen)
                    {
                        return item;
                    }

                    item.Dispose();
                }
            }
            return ObjectGenerator();
        }

    }
}
